package com.self.engine.rocketmq;

import com.self.common.param.ExchangeTrade;
import com.self.engine.service.EntrustOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @version v1.0
 * @ClassName: ExchangeTradeListener
 * @Description:
 * @Author: Mac
 */
@Component
@Slf4j
public class ExchangeTradeListener {

    @Autowired
    private EntrustOrderService entrustOrderService ;


    /**
     * @Title: 交易记录写入表中
     * @Description:
     * @Param [exchangeTrades]
     * @Return void
     * @Author Mac
     * @Throws
     */
    @Transactional
    @StreamListener("exchange_trade_in")
    public void writeExchangeTrade(List<ExchangeTrade> exchangeTrades){
        if (CollectionUtils.isEmpty(exchangeTrades)){
            return;
        }
        for (ExchangeTrade trade : exchangeTrades){
            entrustOrderService.writeExchangeTrade(trade);
        }
    }


    /**
     * @Title: 取消委托单
     * @Description:
     * @Param [orderId]
     * @Return void
     * @Author Mac
     * @Throws
     */
    @Transactional
    @StreamListener("cancel_order_in")
    public void receiveCancelOrder(String orderId){
        entrustOrderService.cancleEntrustOrderToDb(orderId);
    }
}
